import os

# todo
keyboard_map = {
    0 : '2000', 1 : '2001', 2 : '2002', 3 : '2003', 4 : '2004', 5 : '2005',
    6 : '2006', 7 : '2007', 8 : '2008', 9 : '2009',
    'A' : '2017', 'A' : '2017', 'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
    'A' : '2017',
}

class DeviceAction:
    _W = 1080
    _H = 1920
    _type = 'default'
    
    def __init__(self, sn) -> None:
        self._sn = sn
        self._hdc = f'hdc -t {self._sn} shell '
    
    def hdc(self, cmd):
        '''执行hdc shell命令'''
        os.system(self._hdc + cmd)

    def snapshot(self, name=None):
        '''截图'''
        if name:
            self.hdc(f"snapshot_display -f {name}")
        else:
            self.hdc('snapshot_display')
    
    def recv(self, remote, local):
        '''获取设备中的文件'''
        os.system(f'hdc -t {self._sn} file recv {remote} {local}')
    
    def rm(self, file):
        '''删除设备中的文件'''
        self.hdc(f'rm -rf {file}')

    def get_shot(self, shot_name, local_path):
        '''获取设备截图到本地路径'''
        remote_path = '/data/' + shot_name
        self.snapshot(remote_path)
        self.recv(remote_path, local_path)
        self.rm(remote_path)

    def reboot(self):
        '''重启设备'''
        self.hdc('reboot')
    
    def reset(self):
        '''重置设备'''
        self.rm(f'/data/*')
        self.reboot()
    
    def click(self, pos:tuple):
        '''点击某个位置'''
        self.hdc(f'uinput -T -c {pos[0]} {pos[1]}')
    
    def swipe(self, pos1, pos2:tuple=(), time:int=300):
        '''滑动设备屏幕'''
        if type(pos1) == str:
            self.hdc(f'uinput -T -m {pos1}')
        else:
            self.hdc(f'uinput -T -m {pos1[0]} {pos1[1]} {pos2[0]} {pos2[1]} {time}')
    
    def hold_press(self, pos:tuple, time:int=500):
        '''长按'''
        self.hdc(f'uinput -T -d {pos[0]} {pos[1]} -i {time} -u {pos[0]} {pos[1]}')
    
    def drag(self, pos1, pos2:tuple=(), hold_time:int=500, time:int=1000):
        '''拖拽'''
        if type(pos1) == str:
            self.hdc(f'uinput -T -g {pos1}')
        else:
            if hold_time < 500 or time - hold_time < 500:
                print('ERROR：按住时间和拖拽时间都不能小于500ms。')
            self.hdc(f'uinput -T -g {pos1[0]} {pos1[1]} {pos2[0]} {pos2[1]} {hold_time} {time}')
    
    def keyboard(self, input):
        pass
    
    @property
    def W(cls):
        return cls._W
    
    @property
    def H(cls):
        return cls._H
    
    @property
    def type(cls):
        return cls._type

class RK3568(DeviceAction):
    _W = 720
    _H = 1280
    _type = 'RK3568'

    def back_home(self):
        '''返回桌面'''
        self.hdc('uinput -T -c 360 1245')